package com.zyb.eduservice.controller;


import com.zyb.commonutils.R;
import com.zyb.eduservice.entity.EduChapter;
import com.zyb.eduservice.entity.chapter.ChapterVo;
import com.zyb.eduservice.service.EduChapterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testJava
 * @since 2022-04-02
 */
@RestController
@RequestMapping("/eduservice/chapter")
//@CrossOrigin
public class EduChapterController {

    @Resource
    private EduChapterService chapterService;

    @GetMapping("getChapterVideoById/{courseId}")
    public R getChapterList(@PathVariable("courseId") String courseId){
        List<ChapterVo> list=chapterService.getAllChapterVideo(courseId);
        return R.ok().data("allChapterVideo",list);
    }

    //添加章节
    @PostMapping("addChapter")
    public R addChapter(@RequestBody EduChapter eduChapter){
        chapterService.save(eduChapter);
        return R.ok();
    }

    //根据ID章节
    @GetMapping("getChapterInfo/{chapterId}")
    public R addChapter(@PathVariable String chapterId){
        EduChapter eduChapter = chapterService.getById(chapterId);
        return R.ok().data("eduChapter",eduChapter);
    }

    //修改章节
    @PostMapping("updateChapter")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        chapterService.updateById(eduChapter);
        return R.ok();
    }

    //删除章节
    @DeleteMapping("{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean flag=chapterService.deleteChapter(chapterId);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

